package com.mlnx.sim.ecg;


import com.alibaba.fastjson.JSON;
import com.mlnx.common.utils.MyLog;
import com.mlnx.sim.ecg.config.Config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;


public class TestClient {

    static MyLog log = MyLog.getLog(TestClient.class);

    static class Ecg {
        private byte[] data;    // 原始数据

        public byte[] getData() {
            return data;
        }

        public void setData(byte[] data) {
            this.data = data;
        }
    }

    public static List<byte[]> readFile(File file) throws IOException {
        Reader reader = new FileReader(file);
        StringBuffer buffer = new StringBuffer();
        char[] chars = new char[1000];
        int len = 0;
        while ((len = reader.read(chars)) != -1) {
            buffer.append(chars, 0, len);
        }

        List<Ecg> ecgs = JSON.parseArray(buffer.toString(), Ecg.class);

        List<byte[]> bytes = new ArrayList<>();
        for (Ecg ecg : ecgs) {
            int index = 0;
            while (true) {
                if (ecg.getData().length >= index + 24) {
                    byte[] bytes1 = new byte[24];
                    for (int j = 0; j < bytes1.length; j++) {
                        bytes1[j] = ecg.getData()[index++];
                    }
                    bytes.add(bytes1);
                } else {
                    System.out.println("多余：" + (ecg.getData().length - index));
                    break;
                }
            }
        }

        return bytes;
    }

    public static void print(List<PushEcgTimerTask> pushEcgTimerTasks) {

        Collections.sort(pushEcgTimerTasks, new Comparator<PushEcgTimerTask>() {
            @Override
            public int compare(PushEcgTimerTask o1, PushEcgTimerTask o2) {
                return (int) (o1.getLastSucesstime() - o2.getLastSucesstime());
            }
        });
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss SSS");
        int i = 0;
        for (PushEcgTimerTask pushEcgTimerTask : pushEcgTimerTasks) {
            log.info("{} {} 时间:{} 延迟时间:{} 未成功数量:{}",
                    ++i,
                    pushEcgTimerTask.getDeviceId(),
                    format.format(new Date(pushEcgTimerTask.getLastSucesstime())),
                    System.currentTimeMillis() - pushEcgTimerTask.getLastSucesstime(),
                    pushEcgTimerTask.getSendRecord().size());
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {

//        double BPD = 8.6;
//        double AC = 31.7;
//        double FL = 6.9;
//        System.out.println(1.07 * BPD * BPD * BPD + 0.3 * AC * AC * FL);

        List<PushEcgTimerTask> pushEcgTimerTasks = new LinkedList<>();

        if (args.length == 4) {
            Integer start = Integer.valueOf(args[0]);
            Integer end = Integer.valueOf(args[1]);

            String ip = args[2];
            Config.HOST = ip;
            String filePath = args[3];

            System.out.println(String.format("ip:%s  start:%d   end:%d   filePath:%s", ip, start, end, filePath));

            List<byte[]> list = readFile(new File(filePath));

            for (int i = start; i < end; i++) {
                PushEcgTimerTask pushEcgTimerTask = new PushEcgTimerTask("SIMECG000" + i, list);
                pushEcgTimerTasks.add(pushEcgTimerTask);
                new Timer().schedule(pushEcgTimerTask, 0, 500);
            }
        } else {
            Integer start = 101;
            Integer end = 102;

            String ip = "localhost";
            Config.HOST = ip;
            File f = new File("");
            System.out.println(f.getAbsolutePath());
            String filePath = "ecgData.txt";

            System.out.println(String.format("ip:%s  start:%d   end:%d   filePath:%s, path:%s", Config.HOST, start,
                    end,
                    filePath, new File(filePath).getAbsolutePath()));

            List<byte[]> list = readFile(new File(filePath));

            for (int i = start; i < end; i++) {
                PushEcgTimerTask pushEcgTimerTask = new PushEcgTimerTask("SIMECG000" + i, list);
                pushEcgTimerTasks.add(pushEcgTimerTask);
                new Timer().schedule(pushEcgTimerTask, 0, 500);
            }
        }

        while (true) {
            InputStreamReader is = new InputStreamReader(System.in); //new构造InputStreamReader对象
            BufferedReader br = new BufferedReader(is); //拿构造的方法传到BufferedReader中
            String str = br.readLine();

            if (str.equals("p")) {
                try {
                    print(pushEcgTimerTasks);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }

    }
}
